#   库的引用
import PyQt6.QtWidgets as QtWidgets
import PyQt6.QtGui as QtGui

#   软件设置
class TaskManager(QtWidgets.QFrame):
    def __init__(self):
        super().__init__()
        self.setStyleSheet("QFrame{background: white; border-radius: 6px; margin-left: 3px; margin-top: 6px; margin-right: 6px; margin-bottom: 6px}")
        self.initUI()

    def initUI(self):
        self.detail_Dict = dict()
        self.vbox = QtWidgets.QVBoxLayout()
        self.vbox.setContentsMargins(3, 0, 0, 3)
        self.vbox.setSpacing(3)
        self.setLayout(self.vbox)

        #   任务列表
        self.task_List = QtWidgets.QListWidget()
        self.task_List.setStyleSheet("QListWidget{border: 1px solid #BEBEBE;  border-radius: 6px; font: 20px; outline-style: none}"
                                     "QListWidget::item{background: #e0f0e9; border-radius: 6px; margin-top: 3px; margin-left: 3px; margin-right: 3px; height:30}"
                                     "QListWidget::item:hover{background: #c0ebd7}"
                                     "QListWidget::item:selected{background: #c0ebd7; color: black}")
        self.vbox.addWidget(self.task_List)

        #   控制按钮
        self.control_Widget = QtWidgets.QWidget()
        self.control_Layout = QtWidgets.QHBoxLayout()
        self.control_Layout.setContentsMargins(0, 0, 6, 0)
        self.control_Widget.setLayout(self.control_Layout)
        self.vbox.addWidget(self.control_Widget)

        self.info = QtWidgets.QLabel("已加载0条任务")
        self.control_Layout.addWidget(self.info)
        self.control_Layout.addStretch(1)
        
        self.btnDel = QtWidgets.QPushButton("删除")
        self.btnDel.setIcon(QtGui.QIcon("Icon/del.png"))
        self.control_Layout.addWidget(self.btnDel)

        self.btnDetail = QtWidgets.QPushButton("详情")
        self.btnDetail.setIcon(QtGui.QIcon("Icon/detail.png"))
        self.btnDetail.clicked.connect(self.showInfo)
        self.control_Layout.addWidget(self.btnDetail)

        self.btnChange = QtWidgets.QPushButton("修改")
        self.btnChange.clicked.connect(self.showInfo)
        self.control_Layout.addWidget(self.btnChange)

    #   添加条目
    def addItem(self, id, title, describe):
        self.detail_Dict[title] = [id, describe]
        self.task_List.addItem(title)

    #   删除条目
    def delItem(self):
        if self.task_List.currentItem() == None:
            return 0
        else:
            selected_item = self.task_List.currentItem()
            title = selected_item.text()
            self.detail_Dict.pop(title)
            row = self.task_List.currentRow()
            self.task_List.takeItem(row)
        
    #   详细信息
    def showInfo(self):
        if self.task_List.currentItem() == None:
            print("未选中任何项目")
            return 0
        else:
            selected_item = self.task_List.currentItem()
            selected_text = selected_item.text()
            selected_id = self.detail_Dict[selected_text][0]
            selected_describe = self.detail_Dict[selected_text][1]

            print(selected_text)
            print(selected_id)
            print(selected_describe)